home *** CD-ROM | disk | FTP | other *** search
- Path: fas-news.harvard.edu!course2!martino
- From: martino@course2.harvard.edu (Carlo Martino)
- Newsgroups: comp.lang.c
- Subject: [Q] functions returning structures
- Date: 3 Mar 1996 01:28:19 GMT
- Organization: Harvard University, Cambridge, Massachusetts
- Message-ID: <4hasjj$opf@decaxp.harvard.edu>
- NNTP-Posting-Host: course2.harvard.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Is it bad for a function to return a structure? I have been programming in
- C for upwards of 6 years, and only recently did I hear something to the effect
- that structures returned by functions have a tendency to be corrupted.
-
- Then, today, it actually happened to me.,,,
-
- I am using a structure defined in my header as:
-
- typedef struct rgb
- {
- unsigned char r;
- unsigned char g;
- unsigned char b;
- } RGB;
-
- "get_color()" was declared in my header as:
-
- RGB get_color( VECTOR position );
-
- And I'd call it like so:
-
- new_color = get_color( new_position );
-
- This work fine in SunOS. But when I ported this morning to HP-UX, the
- value assigned to "new_color" was always screwy. I walked through it
- in gdb, and the values within "get_color" were precisely what they ought
- to be; it was only the returned structure that was mess up.
-
- So I changed things:
-
- RGB* get_color( VECTOR position );
-
- Which requires an extra free after I'm done with the values referenced by
- the returned pointer. Not to mention the fact that screwing around with
- the pointers, when all I real want is the values, is considerably less
- elegant.
-
- What was going wrong? Is there a way to return structures without putting
- them on the heap? When am I at risk for structures to become corrupt and
- when am I OK?
-
- Needless to say, any enlightenment would be greatly appreciated.
-
- Thanking you in advance,
-
- Carlo Martino
-
-
-